<?php

namespace App\Http\Controllers\Auth;

use App\Socialite;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Auth\Events\Registered;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware(['sms', 'guest']);
        $this->middleware(['oauth'])->only('showRegistrationForm');
    }


    public function showRegistrationForm()
    {
        $socialite = session('socialite');
        $user = session('oauth_user');
        return view('auth.register', compact('user', 'socialite'));
    }

    /**
     * Handle a registration request for the application.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));

        $this->guard()->login($user);

        return $this->registered($request, $user)
            ?: redirect($this->redirectPath());
    }


    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'phone' => 'required|string|max:255|unique:users',
//            'password' => 'required|string|min:6|confirmed',
        ]);
    }


    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        $fill = [
            'name' => $data['name'],
            'phone' => $data['phone'],
            'password' => '',
        ];

        if (array_get($data, 'avatar')) {
            $fill['gender'] = $data['gender'];
            $fill['avatar'] = $data['avatar'];
//          $fill['openid'] = $data['openid'];
        }

        $user = User::create($fill);
        if ($user && $socialiteId = array_get($data, 'socialite_id')) {
            $socialite = Socialite::find($socialiteId);
            if ($socialite) {
                $socialite->user_id = $user->id;
                $socialite->save();
            }
        }
        return $user;
    }
}
